package com.xmtlxx.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.wms.domain.InventoryLog;
import com.xmtlxx.wms.domain.bo.InventoryLogBo;
import com.xmtlxx.wms.domain.vo.InventoryLogVo;
import com.xmtlxx.wms.mapper.InventoryLogMapper;
import com.xmtlxx.wms.service.IInventoryLogService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 库存记录Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class InventoryLogServiceImpl implements IInventoryLogService {

    private final InventoryLogMapper baseMapper;

    /**
     * 查询库存记录
     *
     * @param id 库存记录主键
     * @return 库存记录
     */
    @Override
    public InventoryLogVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectInventoryLogVoByID(id);
    }

    @Override
    public InventoryLogVo queryByReferId(String id,String referId) {
        LambdaQueryWrapper<InventoryLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(id),InventoryLog::getStockId,id);
        lqw.eq(StringUtils.isNotBlank(referId),InventoryLog::getReferId,referId);
        InventoryLog inventoryLog = baseMapper.selectOne(lqw);
        InventoryLogVo vo = new InventoryLogVo();
        BeanUtil.copyProperties(inventoryLog,vo);
        return vo;
    }

    /**
     * 查询库存记录列表
     *
     * @param bo 库存记录
     * @return 库存记录
     */
    @Override
    public TableDataInfo<InventoryLogVo> queryPageList(InventoryLogBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<InventoryLog> lqw = buildQueryWrapper(bo);
       // Page<InventoryLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<InventoryLogVo> result = baseMapper.selectPageInventoryLogList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询库存记录列表
     *
     * @param bo 库存记录
     * @return 库存记录
     */
    @Override
    public List<InventoryLogVo> queryList(InventoryLogBo bo) {
       // LambdaQueryWrapper<InventoryLog> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectInventoryLogList(bo);
    }

    private LambdaQueryWrapper<InventoryLog> buildQueryWrapper(InventoryLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InventoryLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStockId()), InventoryLog::getStockId, bo.getStockId());
        lqw.eq(bo.getStoreId() != null, InventoryLog::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), InventoryLog::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getReferId()), InventoryLog::getReferId, bo.getReferId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), InventoryLog::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), InventoryLog::getStatus, bo.getStatus());
        lqw.eq(bo.getDeptId() != null, InventoryLog::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), InventoryLog::getCreateBy, bo.getCreateBy());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            InventoryLog::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), InventoryLog::getUpdateBy, bo.getUpdateBy());
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            InventoryLog::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), InventoryLog::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增库存记录
     *
     * @param bo 库存记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(InventoryLogBo bo) {
        InventoryLog add = BeanUtil.toBean(bo, InventoryLog.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改库存记录
     *
     * @param bo 库存记录
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(InventoryLogBo bo) {
        InventoryLog update = BeanUtil.toBean(bo, InventoryLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(InventoryLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除库存记录
     *
     * @param ids 需要删除的库存记录主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
